package spider;

import com.alibaba.fastjson.JSONArray;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * �е��Ʊʼ�ͬ�����ع���
 * ����cookie����
 */
public class youdaoSpider {

    static String cookie = "__yadk_uid=Wsl86x1ag1fUW1FgvqfQh4CiXySH5Fzn; JSESSIONID=aaal0FXcO9ni-hEq6hMDx; OUTFOX_SEARCH_USER_ID_NCOO=63935720.11440552; OUTFOX_SEARCH_USER_ID=\"788798331@10.169.0.82\"; YNOTE_CSTK=ODX5i6P-; _ga=GA1.2.2142409683.1612706293; YNOTE_SESS=v2|k4dpPzzoT0kE0Lz50HTy0Qy64g4OMYE0PK0HJunMO50pZOLPzhMzG0OY0Mq40M6y0PLnfJK64U5RTun4zfnfwu0wBnHUAhLwB0; YNOTE_PERS=v2|cqq||YNOTE||web||7776000000||1616056471070||117.136.75.162||qq2A958C8A7B4A1E9F8B4F3AC548B4B2BC||kAOLTunHzW0kMkfO56LzGRlWnHU5hMTz0lMhHO56LpyReu6LpFRLUl0OlOfpBnfOW0wBkfUG0HwZ0ez0HQFhLYY0; JSESSIONID=abc9wVDNv1vMS4Jx1QsIx; _gid=GA1.2.1668907796.1617460950; YNOTE_LOGIN=5||1617460950486; _gat=1";
    static String host = "note.youdao.com";
    static String filePath = "";
    static String bathPath = "E:\\Desktop\\�е��ƱʼǱ���";

    public static void main(String[] args) throws IOException {
        getAllFileDir();
    }


    /**
     * ��ȡ�е��Ƹ�Ŀ¼�������ļ�
     *
     * @throws IOException
     */
    static void getAllFileDir() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://note.youdao.com/yws/api/personal/file?method=listEntireByParentPath&keyfrom=web&cstk=ODX5i6P-");
        httpPost.setHeader("Cookie", cookie);
        httpPost.setHeader("Host", host);

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("path", "/"));
//        nvps.add(new BasicNameValuePair("dirOnly","true"));
//        nvps.add(new BasicNameValuePair("f","true"));
//        nvps.add(new BasicNameValuePair("cstk","ODX5i6P-"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        CloseableHttpResponse res = httpClient.execute(httpPost);
        HttpEntity entity = res.getEntity();
        String html = "";
        if (entity != null) {
            html = EntityUtils.toString(entity, Charset.forName("gb2312"));
//            System.out.println(html);
            JSONArray jsonArray = JSONArray.parseArray(html);
            jsonArray.forEach(j -> {
//            Object j = jsonArray.get(0);
                Map<String, String> map = ((Map<String, Map<String, String>>) j).get("fileEntry");
                filePath = "";
                filePath += map.get("name");
//                System.out.println(map);
                try {
                    if (((Object) map.get("dir")).equals(false)) {
                        getFile(map.get("id"));
                    }else{
                        getFileDir(map.get("id"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                System.out.println(map.get("name"));
            });
        }
//        System.out.println("end");
    }


    /**
     * ��ȡ�е��ƶ���Ŀ¼�������ļ�
     *
     * @throws IOException
     */
    static void getFileDir(String id) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://note.youdao.com/yws/api/personal/file/" + id + "?method=listEntireByParentId&keyfrom=web&cstk=ODX5i6P-");

        httpPost.setHeader("Cookie", cookie);
        httpPost.setHeader("Host", host);

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("fileId", id));
//        nvps.add(new BasicNameValuePair("dirOnly","true"));
//        nvps.add(new BasicNameValuePair("f","true"));
//        nvps.add(new BasicNameValuePair("cstk","ODX5i6P-"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        CloseableHttpResponse res = httpClient.execute(httpPost);
        HttpEntity entity = res.getEntity();
        String html = "";
        if (entity != null) {
            html = EntityUtils.toString(entity, Charset.forName("gb2312"));
//            System.out.println(html);
            JSONArray jsonArray = JSONArray.parseArray(html);
            String tempPath=filePath;
            jsonArray.forEach(j -> {
//                Object j = jsonArray.get(0);
                Map<String, String> map = ((Map<String, Map<String, String>>) j).get("fileEntry");
//                System.out.println(map);
//                System.out.println(map.get("name"));
//                System.out.println((Object) map.get("dir"));
                try {
                    filePath=tempPath;
                    if (((Object) map.get("dir")).equals(false)) {
//                    ��ȡ����
                        filePath += File.separator + map.get("name").split("\\.")[0] + ".html";
//                        System.out.println(filePath);
                        getFile(map.get("id"));
                    } else {
                        filePath += File.separator + map.get("name");
                        getFileDir(map.get("id"));

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
//        System.out.println("end");
    }


    /**
     * ��ȡ�е����ļ�����
     *
     * @throws IOException
     */
    static void getFile(String id) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://note.youdao.com/yws/api/personal/sync?method=download&keyfrom=web&cstk=ODX5i6P-");

        httpPost.setHeader("Cookie", cookie);
//        httpPost.setHeader("Content-Type"," application/x-www-form-urlencoded;charset=UTF-8");
        httpPost.setHeader("Host", host);

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("fileId", id));
//        nvps.add(new BasicNameValuePair("editorType","1")); //1��xml,��������html
//        nvps.add(new BasicNameValuePair("editorVersion","1611196060000"));
//        nvps.add(new BasicNameValuePair("cstk","ODX5i6P-"));
//        nvps.add(new BasicNameValuePair("convert","true"));
        nvps.add(new BasicNameValuePair("version", "-1"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        CloseableHttpResponse res = httpClient.execute(httpPost);
        HttpEntity entity = res.getEntity();
        String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
                "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"zh-CN\" lang=\"zh-CN\"> \n" +
                "<head> \n" +
                "<title>Setting File Title</title> \n" +
                "</head>\n" +
                "<body>\n";
        if (entity != null) {
            html += EntityUtils.toString(entity, Charset.forName("gb2312"));
            html += "\n</body>\n" +
                    "</html>";
//            System.out.println(html);
            System.out.println(filePath);
            File file = new File(bathPath, filePath);
            if (file.getParentFile() != null)
                file.getParentFile().mkdirs();
            new FileOutputStream(file).write(html.getBytes());
        }
        System.out.println("end");
    }
}
